#include "statisticfielddialog.h"
#include "ui_statisticfielddialog.h"

StatisticFieldDialog::StatisticFieldDialog(GeoMap *mapIn,QWidget *parent) :
    QDialog(parent),map(mapIn),
    ui(new Ui::StatisticFieldDialog)
{
    ui->setupUi(this);
    isLoaded=false;


    iniMainWindow();

}

StatisticFieldDialog::~StatisticFieldDialog()
{
    delete ui;
}

void StatisticFieldDialog::iniMainWindow()
{
    curFeatureLayer=new GeoFeatureLayer();
    pEngViewBar=new QWebEngineView();
    pEngViewBar->setContextMenuPolicy(Qt::NoContextMenu);
    pEngViewBar->setUrl(QUrl("file:///home/zgq/CodingWorkspace/Qt Creator/MyScene/Resources/echarts/EChartTest.html"));
    comboCurLayer=new QComboBox();
    connect(comboCurLayer,SIGNAL(currentIndexChanged(int)),
            this,SLOT(on_comboCurLayer_currentIndexChanged(int)));
    comboCurField=new QComboBox();
    connect(comboCurField,SIGNAL(currentIndexChanged(int)),
            this,SLOT(on_comboCurField_currentIndexChanged(int)));

    connect(pEngViewBar,SIGNAL(loadFinished(bool)),
            this,SLOT(on_ResizeEcharts()));

    QLabel *labLayer=new QLabel(QObject::tr("Layer"));
    QLabel *labField=new QLabel(QObject::tr("Field"));
    leftFrame=new QFrame();
    formLayout=new QFormLayout(leftFrame);
    formLayout->setSpacing(6);
    formLayout->setContentsMargins(11,11,11,11);
    formLayout->setWidget(0,QFormLayout::LabelRole,labLayer);
    formLayout->setWidget(0,QFormLayout::FieldRole,comboCurLayer);
    formLayout->setWidget(1,QFormLayout::LabelRole,labField);
    formLayout->setWidget(1,QFormLayout::FieldRole,comboCurField);

    int layersCount=map->getNumLayers();
    for(int i=0;i<layersCount;++i)
    {
        GeoLayer *layer=map->getLayerById(i);
        QString layerName=layer->getName();
        comboCurLayer->addItem(layerName);
    }
    QHBoxLayout *mainLayout=new QHBoxLayout();
    QSplitter *splitter=new QSplitter(Qt::Horizontal,this);
    splitter->addWidget(leftFrame);
    splitter->addWidget(pEngViewBar);
    splitter->setStretchFactor(0,1);
    splitter->setStretchFactor(1,2);
    mainLayout->addWidget(splitter);
    this->setLayout(mainLayout);
    //comboCurLayer->setCurrentIndex(0);
}

//slots

void StatisticFieldDialog::on_comboCurLayer_currentIndexChanged(int index)
{
    curFeatureLayer=map->getLayerByLID(index)->toFeatureLayer();
    QStringList fieldList=curFeatureLayer->getFieldList();
    int count=comboCurField->count();
    qDebug()<<"count:"<<count;
    for(int i=0;i<count-1;++i)
    {
        comboCurField->removeItem(0);
    }
    //comboCurField->removeItem(0);
    //comboCurField=new QComboBox();

    comboCurField->addItems(fieldList);
    comboCurField->removeItem(0);
}
void StatisticFieldDialog::on_comboCurField_currentIndexChanged(int index)
{
    //QString curLayerName=curFeatureLayer->getName();
    QString curFieldName=comboCurField->currentText();
    int featureCount=curFeatureLayer->getFeatureCount();
    QMap<QString,int> kindMap;

    for(int iFeature=0;iFeature<featureCount;++iFeature)
    {
        GeoFeature *feature=curFeatureLayer->getFeature(iFeature);
        GeoFieldDefn *fieldDefn=curFeatureLayer->getFieldDefn(index);
        GeoFieldType fieldType=fieldDefn->getType();
        switch (fieldType)
        {
        default:
            break;
        case kFieldInt:{
            int value;
            feature->getField(index,&value);
            QString value_str=QString::number(value);
            if(kindMap.contains(value_str)){
                kindMap[value_str]+=1;
            }
            else{
                kindMap.insert(value_str,1);
            }
            break;
        }
        case kFieldDouble:{
            double value;
            feature->getField(index,&value);
            QString value_str=QString::number(value);
            if(kindMap.contains(value_str)){
                kindMap[value_str]+=1;
            }
            else{
                kindMap.insert(value_str,1);
            }
            break;
        }
        case kFieldText:{
            QString value;
            feature->getField(index,&value);
            QString value_str=value;
            if(kindMap.contains(value_str)){
                kindMap[value_str]+=1;
            }
            else{
                kindMap.insert(value_str,1);
            }
            break;
        }
        }// end switch
    }// end for all feature
    //statistic all results
    QJsonObject seriesData;
    QJsonArray xAxisData={};
    QJsonArray yAxisData={};
    QMap<QString, int>::const_iterator i = kindMap.constBegin();
    while (i != kindMap.constEnd()){
        QJsonValue xTemp=QJsonValue(i.key());
        QJsonValue yTemp=QJsonValue(i.value());
        ++i;
        xAxisData.append(xTemp);
        yAxisData.append(yTemp);
    }
    seriesData.insert("xAxisData",xAxisData);
    seriesData.insert("yAxisData",yAxisData);

    QString optionStr=QJsonDocument(seriesData).toJson();
    QString js=QString("initBar(%1)").arg(optionStr);
    pEngViewBar->page()->runJavaScript(js);
}

void StatisticFieldDialog::resizeEvent(QResizeEvent *event)
{
    if(isLoaded)
    {
        on_ResizeEcharts();
    }
}

void StatisticFieldDialog::on_ResizeEcharts()
{
    isLoaded=true;
    QJsonObject sizeData;
    //sizeData.insert("width",pEngViewBar->width()-20);
    //sizeData.insert("height",pEngViewBar->height()-20);
    sizeData.insert("width",pEngViewBar->width()-20);
    sizeData.insert("height",pEngViewBar->height()-20);

    QString size_str=QJsonDocument(sizeData).toJson();
    QString js=QString("setSize(%1)").arg(size_str);

    pEngViewBar->page()->runJavaScript(js);
}

